home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.06 Jun 89 / Supe SubCode / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-19  |  7.1 KB  |  339 lines  |  [TEXT/MPS ]

  1. /**********************************************************************************
  2.  
  3.     mini.windows.c
  4.  
  5. ***********************************************************************************/
  6.  
  7. /* NEEDED TO DEFINE NEW TEXTEDIT ROUTINES IN TEXTEDIT.H */
  8. #define __ALLNU__    
  9.  
  10. #include <QuickDraw.h>
  11. #include <Types.h>
  12. #include <Windows.h>
  13. #include <TextEdit.h>
  14. #include <Controls.h>
  15. #include <Events.h>
  16. #include <fonts.h>
  17. #include <toolutils.h>
  18. #include <memory.h>
  19.  
  20. #include "MiniEdit.h"
  21.  
  22. extern WindowRecord        wRecord;
  23. extern WindowPtr             myWindow;
  24. extern ControlHandle     vScroll;
  25. extern TEHandle              TEH;
  26. extern char                     dirty;
  27. extern Str255                 theFileName;
  28.  
  29. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  30.  
  31. SetUpWindows()
  32. {
  33.     Rect                    viewRect;
  34.     Rect                    vScrollRect;
  35.     TextStyle            Styl;
  36.     
  37.     myWindow = GetNewWindow( windowID,(Ptr)&wRecord, (WindowPtr)-1L );
  38.     SetPort(myWindow);
  39.     
  40.     vScrollRect             = (*myWindow).portRect;
  41.     vScrollRect.left         = vScrollRect.right-15;
  42.     vScrollRect.right     += 1;
  43.     vScrollRect.bottom     -= 14;
  44.     vScrollRect.top         -= 1;
  45.     vScroll                     = NewControl(myWindow, &vScrollRect, 
  46.                                                  "", 1, 0, 0, 0,
  47.                                                  scrollBarProc, 0L);
  48.  
  49.     viewRect                 = qd.thePort->portRect;
  50.     viewRect.right         -= SBarWidth;
  51.     viewRect.bottom         -= SBarWidth;
  52.     InsetRect(&viewRect, 4, 4);
  53.     
  54.     TEH                         = TEStylNew( &viewRect, &viewRect );
  55.     Styl.tsFont             = newYork;
  56.     Styl.tsSize             = 14;
  57.     TESetStyle(doSize+doFont,&Styl,1,TEH);
  58.     
  59.     SetView(qd.thePort);
  60.     dirty = 0;
  61. }
  62.  
  63. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  64.  
  65. AdjustText ()
  66. {
  67.     int        oldScroll, newScroll, delta, value, i;
  68.     register LHPtr            LHptr;
  69.     
  70.     oldScroll     = (**TEH).viewRect.top - (**TEH).destRect.top;
  71.     
  72.     newScroll    = 0;
  73.     value            = GetCtlValue(vScroll);
  74.     LHptr         = *(**GetStylHandle(TEH)).lhTab;
  75.     for (i=0;i<value;i++)
  76.         newScroll += LHptr[i].lhHeight;
  77.  
  78.     delta         = oldScroll - newScroll;
  79.     
  80.     if (delta != 0)
  81.       TEScroll(0, delta, TEH);
  82. }
  83.  
  84. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  85.  
  86. SetVScroll()
  87. {
  88.     short        lines, viewLines, max;
  89.     short        oldValue, oldMax;
  90.     TEPtr        te;
  91.     
  92.     oldValue = GetCtlValue(vScroll);
  93.     oldMax = GetCtlMax(vScroll);
  94.     
  95.     te = *TEH;                            /* point to TERec for convenience */
  96.     
  97.     lines            = te->nLines;
  98.     viewLines    = linesInView(TEH,0);
  99.     
  100.     /* since nLines isn’t right if the last character is a return, check for that case */
  101.     if ( *(*te->hText + te->teLength - 1) == '\r' )
  102.         lines += 1;
  103.     max = lines - viewLines;
  104.     
  105.     if ( max < 0 ) max = 0;
  106.     SetCtlMax(vScroll, max);
  107. }
  108.  
  109. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  110.  
  111. ShowSelect()
  112. {
  113.     register    int        topLine, bottomLine, theLine, lines;
  114.     int                    before,selStart;
  115.     short                    *lStarts;
  116.     
  117. //    SetVScroll();
  118. //    AdjustText();
  119.     
  120.     lines = linesInView(TEH,&before);
  121.     
  122.     topLine         = GetCtlValue(vScroll);
  123.     bottomLine     = topLine + lines;
  124.     
  125.     selStart = (**TEH).selStart;
  126.     lStarts  = (**TEH).lineStarts;
  127.     
  128.     if ((selStart < lStarts[topLine]) || (selStart > lStarts[bottomLine]))
  129.     {
  130.         SetVScroll();
  131.  
  132.         for (theLine = 0, lStarts = (**TEH).lineStarts; 
  133.               lStarts[theLine] < selStart;
  134.               theLine++)
  135.             ;
  136.         
  137.         SetCtlValue(vScroll, theLine - before + lines / 2);
  138.         AdjustText();
  139.     }
  140. }
  141.  
  142. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  143.  
  144. linesInView(TEHdl,above)
  145.     TEHandle        TEHdl;
  146.     int            *above;
  147. {
  148.     register LHHandle        LHHdl;
  149.     register LHPtr            LHptr;
  150.     register int            nLines,before,within;
  151.     Rect                        dest,view;
  152.     
  153.     dest        = (**TEHdl).destRect;
  154.     view        = (**TEHdl).viewRect;
  155.     
  156.     LHHdl     = (**GetStylHandle(TEHdl)).lhTab;
  157.     nLines    = (**TEHdl).nLines;
  158.     before    = 0;
  159.     
  160.     LHptr     = *LHHdl;
  161.     
  162.     while ((dest.top < view.top) && (before < nLines))
  163.         dest.top += LHptr[before++].lhHeight;
  164.     
  165.     within = before;
  166.     view.bottom--;
  167.     
  168.     while ((dest.top < view.bottom) && (within < nLines))
  169.         dest.top += LHptr[within++].lhHeight;
  170.     
  171.     if (above) *above = before;
  172.     within -= before;
  173.     
  174.     return(within);
  175. }
  176.  
  177. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  178.  
  179. SetView(w)
  180.     WindowPtr w;
  181. {
  182.     (**TEH).viewRect                 = w->portRect;
  183.     (**TEH).viewRect.right         -= SBarWidth;
  184.     (**TEH).viewRect.bottom     -= SBarWidth;
  185.     InsetRect(&(**TEH).viewRect, 4, 4);
  186.  
  187.     (**TEH).destRect.right         = (**TEH).viewRect.right;
  188.     
  189.     TECalText(TEH);
  190. }
  191.  
  192. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  193.  
  194. UpdateWindow(theWindow)
  195.     WindowPtr    theWindow;
  196. {
  197.     GrafPtr    savePort;
  198.     
  199.     GetPort( &savePort );
  200.     SetPort( theWindow );
  201.     
  202.     BeginUpdate( theWindow );
  203.         EraseRect(&theWindow->portRect);
  204.         DrawControls( theWindow );
  205.         DrawGrowIcon( theWindow );
  206.         TEUpdate( &theWindow->portRect, TEH );
  207.     EndUpdate( theWindow );
  208.     
  209.     SetPort( savePort );
  210. }
  211.  
  212. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  213.  
  214. pascal void ScrollProc(theControl, theCode)
  215.     ControlHandle    theControl;
  216.     short                theCode;
  217. {
  218.     int    pageSize;
  219.     int    scrollAmt;
  220.     int    lines;
  221.     
  222.     if (theCode == 0)
  223.         return;
  224.         
  225.     lines = linesInView(TEH,NULL);
  226.     
  227.     pageSize = lines - 1;
  228.             
  229.     switch (theCode)
  230.     {
  231.         case inUpButton: 
  232.             scrollAmt = -1;
  233.             break;
  234.         case inDownButton: 
  235.             scrollAmt = 1;
  236.             break;
  237.         case inPageUp: 
  238.             scrollAmt = -pageSize;
  239.             break;
  240.         case inPageDown: 
  241.             scrollAmt = pageSize;
  242.             break;
  243.     }
  244.     
  245.     SetCtlValue( theControl, GetCtlValue(theControl)+scrollAmt );
  246.     AdjustText();
  247.  
  248. }
  249.  
  250. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  251.  
  252. DoContent(theWindow, theEvent)
  253.     WindowPtr    theWindow;
  254.     EventRecord    *theEvent;
  255. {
  256.     int                cntlCode;
  257.     ControlHandle     theControl;
  258.     GrafPtr            savePort;
  259.     
  260.     GetPort(&savePort);
  261.     SetPort(theWindow);
  262.     GlobalToLocal( &theEvent->where );
  263.     
  264.     cntlCode = FindControl(theEvent->where, theWindow, &theControl);
  265.     if (cntlCode == 0)
  266.     {
  267.         if (PtInRect( theEvent->where, &(**TEH).viewRect ))
  268.             TEClick( theEvent->where, (theEvent->modifiers & shiftKey )!=0, TEH);
  269.     }
  270.     else 
  271.         if (cntlCode == inThumb)
  272.         {
  273.             TrackControl(theControl, theEvent->where, 0L);
  274.             AdjustText();
  275.         }
  276.         else
  277.             TrackControl(theControl, theEvent->where, (ProcPtr)ScrollProc);
  278.  
  279.     SetPort(savePort);
  280. }
  281.  
  282. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  283.  
  284. MyGrowWindow( w, p )
  285.     WindowPtr w;
  286.     Point p;
  287. {
  288.     GrafPtr    savePort;
  289.     long        theResult;
  290.     int        oScroll,b,r;
  291.     Rect         rct, oView;
  292.     
  293.     GetPort( &savePort );
  294.     SetPort( w );
  295.  
  296.     b = qd.screenBits.bounds.bottom;
  297.     r = qd.screenBits.bounds.right;
  298.  
  299.     SetRect(&rct, 100, 100, r, b);
  300.     theResult = GrowWindow( w, p, &rct );
  301.     
  302.     if (theResult == 0)
  303.       return;
  304.       
  305.     SizeWindow( w, LoWord(theResult), HiWord(theResult), 1);
  306.  
  307.     InvalRect(&w->portRect);
  308.     oView     = (**TEH).viewRect;
  309.     oScroll     = GetCtlValue(vScroll);
  310.     
  311.     SetView(w);
  312.     HidePen();
  313.     MoveControl(vScroll,
  314.                     w->portRect.right - SBarWidth,
  315.                     w->portRect.top-1);
  316.     SizeControl(vScroll, 
  317.                     SBarWidth+1,
  318.                     w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  319.     ShowPen();
  320.     
  321.     SetVScroll();
  322.     AdjustText();
  323.     
  324.     SetPort( savePort );
  325. }
  326.  
  327. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  328.  
  329. CloseMyWindow()
  330. {
  331.     HideWindow( myWindow );
  332.     TESetSelect( 0, (**TEH).teLength, TEH );
  333.     TEDelete( TEH );
  334.     SetVScroll();
  335. }
  336.  
  337. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  338.  
  339.